﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DatabaseInteraction
/// </summary>
public class DatabaseInteraction
{
    public static List<New_Report> GetAllNews()
    {
        NewReportDataContext data = new NewReportDataContext();
        List<New_Report> list = (from s in data.New_Reports select s).ToList();
        return list;
    }
    public static List<New_Report> GetAllNewsForHomePage(string channelName)
    {
        NewReportDataContext data = new NewReportDataContext();
        List<New_Report> list = (from s in data.New_Reports
                                 where s.ChanelName.Equals(channelName)
                                 orderby s.TimePost descending
                                 select s).Take(4).ToList();
        return list;
    }
    public static int CountNews()
    {
        NewReportDataContext data = new NewReportDataContext();
        int count = (from s in data.New_Reports select s).Count();
        return count;
    }
    public static New_Report GetNewsByID(int id)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = (from s in data.New_Reports
                        where s.NewsID == id
                        select s).Single();
        return n;
    }
    public static void UpdateNewsContent(int id, string content)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = data.New_Reports.Single(s => s.NewsID == id);
        n.NewsContent = content;
        data.SubmitChanges();
    }

    public static string GetNewContentByID(int id)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = data.New_Reports.Single(s => s.NewsID == id);
        return n.NewsContent;
    }
    public static List<Comment> GetComment(int newID)
    {
        NewReportDataContext data = new NewReportDataContext();
        List<Comment> list = (from c in data.Comments
                              where c.NewsID == newID
                              select c).ToList();
        return list;
    }

    public static void UpdateCountComment(int? newID)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = data.New_Reports.Single(s => s.NewsID == newID);
        n.CountComments = n.CountComments + 1;
        data.SubmitChanges();
    }

    public static void AddComment(Comment c)
    {
        NewReportDataContext data = new NewReportDataContext();
        data.Comments.InsertOnSubmit(c);
        UpdateCountComment(c.NewsID);
        data.SubmitChanges();
    }
    public static int? CountViews(int newID)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = data.New_Reports.Single(s => s.NewsID == newID);
        return n.CountRead;
    }
    public static void UpdateCountViews(int newID)
    {
        NewReportDataContext data = new NewReportDataContext();
        New_Report n = data.New_Reports.Single(s => s.NewsID == newID);
        n.CountRead = n.CountRead + 1;
        data.SubmitChanges();
    }
    
}

//void test(int id)
//{
//    NewReportDataContext data = new NewReportDataContext();
//    var q = from c in data.Comments join n in data.New_Reports
//                                    on c.New_Report.NewsID == c.NewsID
//                                    where c.NewsID == id
//                                    select c.CommentContent;
//}